// LoginServiceImpl.java
package com.ruoyi.nongjt.service.impl;

import com.ruoyi.nongjt.domain.Login;
import com.ruoyi.nongjt.domain.NjtUserPerfect;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.domain.LoginResult;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.ILoginService;
import com.ruoyi.nongjt.service.INjtUserPerfectService;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 登录服务实现
 */
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private NjtUserRegisterMapper userRegisterMapper;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private PasswordUtils passwordUtils;
    
    @Autowired
    private INjtUserPerfectService userPerfectService;

    @Override
    public LoginResult login(Login login) {
        // 验证参数
        if (login.getUsername() == null || login.getPassword() == null) {
            return LoginResult.error("用户名和密码不能为空");
        }

        // 查询用户（使用手机号作为用户名）
        NjtUserRegister query = new NjtUserRegister();
        query.setMobilePhone(login.getUsername());
        List<NjtUserRegister> users = userRegisterMapper.selectNjtUserRegisterList(query);

        if (users.isEmpty()) {
            return LoginResult.error("用户不存在");
        }

        NjtUserRegister user = users.get(0);

        // 检查用户状态
        if ("0".equals(user.getUserStatus())) {
            return LoginResult.error("账号已被冻结");
        }

        if ("2".equals(user.getUserStatus())) {
            return LoginResult.error("账号状态异常");
        }

        // 验证密码（这里简单比较，实际应该加密比较）
        // 注意：您需要在注册时对密码进行加密存储
        if (!passwordUtils.matches(login.getPassword(), user.getPassword())) {
            return LoginResult.error("密码错误");
        }

        // 生成token
        String token = jwtUtils.generateToken(login.getUsername());
        
        // 构建登录结果
        LoginResult result = LoginResult.success(token, user);
        
        // 获取并设置用户完善状态
        String perfectStatus = userPerfectService.getPerfectInfoStatus(user.getId());
        result.setPerfectStatus(perfectStatus);
        
        // 如果有完善资料，获取审核意见
        if (!"0".equals(perfectStatus)) {
            NjtUserPerfect perfectInfo = userPerfectService.selectNjtUserPerfectByUserId(user.getId());
            if (perfectInfo != null && perfectInfo.getAuditRemark() != null) {
                result.setAuditRemark(perfectInfo.getAuditRemark());
            }
        }

        return result;
    }

    @Override
    public void logout(String token) {
        // JWT是无状态的，客户端删除token即可
        // 如果需要服务端控制，可以在这里添加token黑名单逻辑
    }

    @Override
    public boolean validateToken(String token) {
        return jwtUtils.validateToken(token);
    }

    @Override
    public String getUserInfo() {
        return "用户信息";
    }
}